package com.papper.common.factory;

import com.google.common.collect.Lists;
import com.papper.common.bean.log.LogOperation;
import com.papper.common.config.PapperStorage;
import com.papper.common.service.log.LogOperationService;
import com.papper.common.utils.tool.StringTool;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.slf4j.helpers.MessageFormatter;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.scheduling.annotation.Scheduled;
import org.springframework.stereotype.Component;

import java.util.Date;
import java.util.List;
import java.util.concurrent.BlockingQueue;
import java.util.concurrent.LinkedBlockingQueue;

/**
 * @Author:Lajiao
 * @Date:2014年10月21日
 * @Time:上午8:23:04
 * @Description:系统日志表
 */
@Component
public class PapperLogTool {
    private static Logger                      logger     = LoggerFactory.getLogger(PapperLogTool.class);
    private static BlockingQueue<LogOperation> matchQueue = new LinkedBlockingQueue<LogOperation>();

    @Autowired
    LogOperationService logOperationService;

    @Scheduled(cron = "* */1 * * * ?")
    public void runFunction() {
        try {
            int size = matchQueue.size();
            if (size <= 0) {
                return;
            }
            List<LogOperation> logOperations = Lists.newArrayList();
            Integer            maxCount      = StringTool.parseInteger(PapperStorage.Config("SysConfig", "papper.log.max", "100"), 100);
            if (size > maxCount) {
                matchQueue.drainTo(logOperations, maxCount);
            } else {
                matchQueue.drainTo(logOperations, size);
            }
            logOperationService.batchInsert(logOperations);
        } catch (Exception ex) {
            logger.error("系统日志入库异常:{}", ex);
        }
    }

    /**
     * 系统业务记录日志.
     *
     * @param desc    the desc
     * @param session the session
     * @param linkId  the link id
     * @param content the content
     * @param objects the objects
     */
    public static void OPER(String desc, String session, String linkId, String content, Object... objects) {
        try {
            LogOperation log = new LogOperation();
            log.setLoggerType("PapperLog");
            log.setLoggerDesc(desc);
            log.setLoggerSeesion(session);
            log.setLoggerLinkid(linkId);
            if (objects != null && objects.length > 0) {
                content = MessageFormatter.arrayFormat(content, objects).getMessage();
            }
            log.setLoggerContent(content.length() > 8096 ? content.substring(0, 8096) : content);
            log.setCreateTime(new Date());
            matchQueue.put(log);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }

    /**
     * 系统异常日志.
     *
     * @param desc    the desc
     * @param session the session
     * @param linkId  the link id
     * @param content the content
     * @param objects the objects
     */
    public static void ERROR(String desc, String session, String linkId, String content, Object... objects) {
        try {
            LogOperation log = new LogOperation();
            log.setLoggerType("ERROR");
            log.setLoggerDesc(desc);
            log.setLoggerSeesion(session);
            log.setLoggerLinkid(linkId);
            if (objects != null && objects.length > 0) {
                content = MessageFormatter.arrayFormat(content, objects).getMessage();
            }
            log.setLoggerContent(content.length() > 8096 ? content.substring(0, 8096) : content);
            log.setCreateTime(new Date());
            matchQueue.put(log);
        } catch (InterruptedException e) {
            e.printStackTrace();
        }
    }
}
